package com.wuyan.locationdog.ui.my

import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import com.amap.api.location.AMapLocationListener
import com.amap.api.maps.model.LatLng
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.bean.TeamMember
import com.arcsoft.commonbase.bus.Bus
import com.arcsoft.commonbase.bus.bus_device_show_mask
import com.arcsoft.commonbase.bus.bus_fence_refresh
import com.arcsoft.commonbase.bus.bus_team_refresh
import com.arcsoft.commonbase.ext.createEmptyView
import com.arcsoft.commonbase.ext.gone
import com.arcsoft.commonbase.ext.visible
import com.arcsoft.commonbase.utils.getTestDataList
import com.blankj.utilcode.util.ActivityUtils
import com.tuo.customview.VerificationCodeView
import com.wuyan.locationdog.R
import com.wuyan.locationdog.adapter.TeamPersonListAdapter
import com.wuyan.locationdog.adapter.TestListAdapter
import com.wuyan.locationdog.databinding.TeamCreatInputBinding
import com.wuyan.locationdog.help.LoginHelp
import com.wuyan.locationdog.help.countDownCoroutines
import com.wuyan.locationdog.util.maputil.MapHelp
import com.wuyan.locationdog.viewmodel.DeviceViewModel


class TeamCreateInputActivity : BaseBindingActivity<TeamCreatInputBinding>() {
    private var latLng: LatLng? = null
    private val mViewModel: DeviceViewModel by viewModels()

    private val mAdapter by lazy { TeamPersonListAdapter(R.layout.item_team_create_img, null) }

    override fun initData() {

        setTitleText("面对面建群")

        binding.apply {
            tvNum.gone()
            div.gone()
            recyclerView.gone()
            btnSure.gone()
            codeView.setInputCompleteListener(object : VerificationCodeView.InputCompleteListener {
                override fun inputComplete() {
                    if (codeView.inputContent.length != 4) {
                        return
                    }
                    tvNum.text = codeView.inputContent

                    recyclerView.apply {
                        layoutManager = GridLayoutManager(context, 5)
                        adapter = mAdapter.apply {

                            setOnItemClickListener { adapter, view, position ->

                            }

                        }
                    }
                    countDownCoroutines(9999, this@TeamCreateInputActivity.lifecycleScope, {
                        val map = HashMap<String, Any>()
                        map["code"] = tvNum.text.toString()
                        map["gdLat"] = latLng?.latitude.toString()
                        map["gdLng"] = latLng?.longitude.toString()
                        mViewModel.getTeamDetailByCode(map)
                    })




                    desc.gone()
                    codeView.gone()

                    tvNum.visible()
                    div.visible()
                    recyclerView.visible()
                    btnSure.visible()

                }

                override fun deleteContent() {

                }
            })

            btnSure.setOnClickListener {
                val map = HashMap<String, Any>()
                map["code"] = tvNum.text.toString()
                map["gdLat"] = latLng?.latitude.toString()
                map["gdLng"] = latLng?.longitude.toString()
                mViewModel.addTeam(map)
            }

        }
        loadNetData()
    }

    override fun loadNetData() {
        super.loadNetData()
        MapHelp.initLocation(mActivity, mLocationListener)
    }

    override fun observe() {
        super.observe()
        Bus.observe<String>(bus_fence_refresh, this) {
            loadNetData()
        }
        mViewModel.mTeamDetail.observe(this) {

            mAdapter.setNewInstance(it.content?.teamMembers?.toMutableList())
            mAdapter.addData(0, TeamMember(userAvatar = LoginHelp.myInfoBean.content.avatarDTO.fileUrl))

        }
        mViewModel.anyBean.observe(this) {
            Bus.post(bus_team_refresh, "")
            finish()
        }
    }

    //声明定位回调监听器
    private var mLocationListener = AMapLocationListener { amapLocation ->
        if (amapLocation != null) {
            if (amapLocation.errorCode == 0) {
                //实时位置上报
                Log.e("map", "定位成功:$amapLocation")
                latLng = LatLng(amapLocation.latitude, amapLocation.longitude)


            } else {
                //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
                Log.e(
                    "map",
                    "location Error, ErrCode:" + amapLocation.errorCode + ", errInfo:" + amapLocation.errorInfo
                )
            }
        }
    }

}